/*
 * Instruction0A.java
 *
 * Created on March 22, 2007, 9:22 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
/*
 
Terminates subroutines in expanded memory invoked by the CALL
instruction. Returns execution flow from the subroutine to the calling
program. The program overlay page (PPAGE) register and the return
address are restored from the stack; program execution continues at the
restored address. For code compatibility purposes, CALL and RTC also
execute correctly in devices that do not have expanded memory
capability.

(M(SP)) => PPAGE
(SP) + $0001 => SP
(M(SP) : M(SP+1)) => PCH : PCL
(SP) + $0002 => SP

 */
package moto9s12sim;

/**
 *
 * @author Fergus Earnshaw
 */
public class Instruction0A extends Instruction {
    
    /** Creates a new instance of Instruction0A */
    public Instruction0A() {
        this.strAssemblyCode = "RTC";
        this.bCodes = new byte[1];
        this.bCodes[0] = (byte)Integer.parseInt("0A",16);
    }
    
    public boolean excute(M9S12 cpu)
    {
        this.shAddr = cpu.PC.getRegister();
        cpu.PC.inc();
       
        
        //pg273 in the guide...
        byte bPage;
        //(SP)) => PPAGE
        bPage = cpu.bMemory[cpu.SP.getUnsignedRegister()];
        //(SP) + $0001 => SP        
        cpu.SP.inc();
        //(M(SP) : M(SP+1)) => PCH : PCL
        cpu.PC.setRegister(bPage, cpu.bMemory[cpu.SP.getUnsignedRegister()]);
        
        //(SP) + $0002 => SP
        
        //once again the addressing high to low...  so pop of the stack...
        cpu.SP.inc();
        
        
        //potential error if SP

        printInstruction(cpu);
        //I think the first invalid instruction should halt excution
        return true;
    }  
    
}
